package com.globallogic.dmytro_zakharenko.university;

import java.util.Arrays;

import com.globallogic.dmytro_zakharenko.university.enums.AuditoriumNames;
import com.globallogic.dmytro_zakharenko.university.enums.DaysOfWeek;
import com.globallogic.dmytro_zakharenko.university.enums.LessonNames;
import com.globallogic.dmytro_zakharenko.university.enums.LessonsTime;

public class Schedule {
	
	private static String lesson;
	private static AuditoriumNames auditorium;
	private final String[] courses = {"1_Course","2_Course", "3_Course"};
	public String[][] schedule = new String[60][4];
	
	public Schedule () {
		this.doSchedule();
	}
	
	/**
     * fill in schedule, each element consists from 4 fields
	 * @throws Exception 
     */
	private void doSchedule() {
		int i = 0;
		for (DaysOfWeek day : DaysOfWeek.values()) {
			for (String course : courses) {
				for (LessonsTime lessonTime : LessonsTime.values()) {
					schedule[i][0] = day.name();
					schedule[i][1] = lessonTime.name();
					schedule[i][2] = getLesson(course);
					schedule[i++][3] = getAuditorium().name();
				}
			}
		}
	}
	
	/**
     * returns schedule, each element consists from 4 fields
     *
     *@return String[][] 
     */
	public String[][] getSchedule() {
		return schedule;
	}
	
	/**
     * returns lesson, find the lesson that haven't been used before. After all lessons have been found do one more circle
     *
     *@param course name 
     */
	public static String getLesson(String course) {
		if (course.equalsIgnoreCase("1_Course") || course.equalsIgnoreCase("2_Course") || course.equalsIgnoreCase("3_Course")) {
			
			try {
				LessonNames.getLessonsName().toString();
			} catch (NullPointerException e) {
				System.out.println("The lessons were not initialized. Initialize to default lessons.");
				LessonNames.setBasicLessons();
			}
			
			String[] ls = LessonNames.getLessonsName();
			int i = 0;
			for (String les : ls) {
				if (les.equalsIgnoreCase(lesson)) {
					break;
				}
				i++;
			}
			
			if (lesson == null || !ls[i + 1].equalsIgnoreCase(course)) {
				if (course.equalsIgnoreCase("1_Course")) {
					lesson = "Astronomy";
				} else if (course.equalsIgnoreCase("2_Course")) {
					lesson = "Charms";
				} else if (course.equalsIgnoreCase("3_Course")) {
					lesson = "Study Of Ancient Runes";
				}
				return lesson;
			}
			boolean bool = false;
			for (int k = 0; k < ls.length; k++) {
				if (bool && ls[k + 1].equalsIgnoreCase(course)) {
					lesson = ls[k];
					return lesson;
				}
				if (lesson.equals(ls[k]) && ls[k + 1].equalsIgnoreCase(course)) {
					bool = true;
				}	
			}
		} 
		
		return null;	
	}
	
	/**
     * returns auditorium, find the auditorium that haven't been used before. After all auditoriums have been found do one more circle
     *
     */
	public static AuditoriumNames getAuditorium() {
		boolean bool = false;
		if (auditorium == null) {
			auditorium = AuditoriumNames.Astronomy_Auditorium;
			return auditorium;
		}
		for (AuditoriumNames a : AuditoriumNames.values()) {
			if (bool) {
				auditorium = a;
				return auditorium;
			}
			if (auditorium.equals(a)) {
				bool = true;
			}	
		}
		
		auditorium = AuditoriumNames.Astronomy_Auditorium;
		return auditorium;	
	}

	@Override
	public int hashCode()  {
		final int prime = 31;
		int result = 1;
		result = prime * result + Arrays.hashCode(courses);
		result = prime * result + Arrays.deepHashCode(schedule);
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Schedule other = (Schedule) obj;
		if (!Arrays.equals(courses, other.courses))
			return false;
		if (!Arrays.deepEquals(schedule, other.schedule))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "Schedule [courses=" + Arrays.toString(courses) + ", schedule="
				+ Arrays.deepToString(schedule) + "]";
	}
}
